package com.mall.seckill_mall.rabbitmq.sender;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class TestSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendOrder(String msg, int ttl) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //发送消息-->交换机：订单交换机；路由键：订单路由键
        rabbitTemplate.convertAndSend("order_exchange", "order", msg, message -> {
            System.out.println("订单创建！" + sdf.format(new Date()) + "----> send");
            message.getMessageProperties().setExpiration(String.valueOf(ttl));
            return message;
        });
    }
}
